home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xjewel / hscore.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  3.4 KB  |  203 lines

  1. /*
  2. **
  3. **    X11 Jewel By David Cooper and Jose Guterman 05/92
  4. **
  5. */
  6.  
  7. #include <stdio.h>
  8.  
  9. #ifndef SYSV
  10. #   include <string.h>
  11. #   ifdef VMS
  12. #    include <file.h>
  13. #   else
  14. #    include <sys/file.h>
  15. #   endif
  16. #else
  17. #   include <sys/types.h>
  18. #   include <sys/stat.h>
  19. #   include <fcntl.h>
  20. #   include <strings.h>
  21. #endif
  22.  
  23. #ifdef GETPWENT
  24. #   include <pwd.h>
  25. #endif
  26.  
  27. #include <errno.h>
  28.  
  29. #include "general.h"
  30. #include "hscore.h"
  31. #include "xhscore.h"
  32.  
  33. int hscorefd;
  34. FILE *hscorefile;
  35.  
  36.  
  37. int num_high_scores;
  38. struct record high_scores[MAX_HIGH_SCORES];
  39.  
  40. static int Stage, Score;
  41.  
  42. void Write_Scores()
  43.     {
  44.     int i;
  45.  
  46.     rewind(hscorefile);
  47.     fflush(hscorefile);
  48.     fprintf(hscorefile, "%d\n", num_high_scores);
  49.     for (i=0; i<num_high_scores; i++)
  50.         {
  51.         fprintf(hscorefile, "%s %d %d\n", high_scores[i].name,
  52.                 high_scores[i].stage, high_scores[i].score);
  53.         }
  54.     fflush(hscorefile);
  55.     }
  56.  
  57. void Read_Scores()
  58.     {
  59.     int i;
  60.  
  61.     rewind(hscorefile);
  62.     fscanf(hscorefile, "%d\n", &num_high_scores);
  63.     for (i=0; i<num_high_scores; i++)
  64.         {
  65.         fscanf(hscorefile, "%s %d %d\n", high_scores[i].name,
  66.                 &high_scores[i].stage, &high_scores[i].score);
  67.         high_scores[i].name[MAX_NAME_LENGTH] = '\0';
  68.         }
  69.     }
  70.  
  71. void File_Lock()
  72.     {
  73.     }
  74.  
  75. void File_Unlock()
  76.     {
  77.     }
  78.  
  79. BOOL Open_High_Score_File()
  80.     {
  81.     if ((hscorefd = open(HSCORE_FILE, O_RDWR)) <0)
  82.         {
  83.         if (errno == ENOENT) 
  84.             {
  85.             /* File  does no exist */
  86.             if ((hscorefd = open(HSCORE_FILE, O_RDWR|O_CREAT, 0666)) <0)
  87.                 {
  88.                 /* Unsuccesful creation of file */
  89.                 perror("CANNOT OPEN HIGHSCOREFILE:");
  90.                 return(FALSE);
  91.                 }
  92.             else
  93.                 {
  94.                 /* Succesful creation of new file */
  95.                 File_Lock();
  96.                 hscorefile = fdopen(hscorefd, "r+");
  97.                 num_high_scores = 0;
  98.                 Write_Scores();
  99.                 return(TRUE);
  100.                 }
  101.             }
  102.         else
  103.             {
  104.             /* File exist and unsuccesful open */
  105.             return(FALSE);
  106.             }
  107.         }
  108.     else
  109.         {
  110.         /* File exists and succesful open */
  111.         File_Lock();
  112.         hscorefile = fdopen(hscorefd, "r+");
  113.         Read_Scores();
  114.         return(TRUE);
  115.         }
  116.     }
  117.  
  118. void Close_High_Score_File()
  119.     {
  120.     File_Unlock();
  121.     fclose(hscorefile);
  122.     close(hscorefd);
  123.     }
  124.  
  125. void Add_High_Score(i)
  126. int i;
  127.     {
  128.     high_scores[i].stage = Stage;
  129.     high_scores[i].score = Score;
  130. #ifndef GETPWENT
  131.     /*printf("%s\n",cuserid(0l));*/
  132.     memcpy(high_scores[i].name,cuserid(0l),8);
  133. #else
  134.     memcpy(high_scores[i].name,getpwuid(getuid())->pw_name,MAX_NAME_LENGTH+1);
  135. #endif
  136.     if (num_high_scores < MAX_HIGH_SCORES)
  137.         {
  138.         num_high_scores++;
  139.         }
  140.     Write_Scores();
  141.     Draw_One_High_Score(i+1);
  142.     }
  143.  
  144. void Update_High_Scores(NewStage, NewScore)
  145. int NewStage, NewScore;
  146.     {
  147.     int i, j;
  148.     BOOL updated;
  149.     
  150.     Stage=NewStage;
  151.     Score=NewScore;
  152.  
  153.     if (!Open_High_Score_File())
  154.         {
  155.         printf("Could not open high scores file\n");
  156.         }
  157.  
  158.     Show_High_Scores(1);
  159.  
  160.     updated = FALSE;
  161.     for (i=0; i<num_high_scores; i++)
  162.         {
  163.         if (Score > high_scores[i].score)
  164.             {
  165.             for (j=MAX_HIGH_SCORES-2; j>=i; j--)
  166.                 {
  167.                 high_scores[j+1].stage = high_scores[j].stage;
  168.                 high_scores[j+1].score = high_scores[j].score;
  169.                 memcpy(high_scores[j+1].name, high_scores[j].name,
  170.                         MAX_NAME_LENGTH+1);
  171.                 }
  172.             if (num_high_scores == MAX_HIGH_SCORES)
  173.                 {
  174.                 Wipeout_Last_High_Score();
  175.                 }
  176.             Move_Down_High_Scores(i+1);
  177.             /*ms_sleep(1000L);*/
  178.             Add_High_Score(i);
  179.             Show_High_Scores(i+1);
  180.             updated = TRUE;
  181.             break;
  182.             }
  183.         }
  184.  
  185.     if ((!updated) && (num_high_scores < MAX_HIGH_SCORES))
  186.         {
  187.         Add_High_Score(num_high_scores);
  188.         }
  189.     
  190.     Close_High_Score_File();
  191.     }
  192.  
  193. void Refresh_High_Scores()
  194.     {
  195.     if (!Open_High_Score_File())
  196.         {
  197.         printf("Could not open high scores file\n");
  198.         }
  199.  
  200.     Show_High_Scores(1);
  201.     Close_High_Score_File();
  202.     }
  203.